package bddObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.util.Log;



public class RestApi {
	public static String adress = "http://192.168.1.100/magy" ;
	
	
	public String RestRequete(String method, String params){
		String res = null ;
		InputStream is = null ;
		try {
			URL url = (params != null)? new URL(adress+"/?method="+method+"&"+params) : new URL(adress+"/?method="+method);
			Log.d("ADRESS ===> ",adress+"/?method="+method );
			HttpURLConnection conn;
			
			conn = (HttpURLConnection) url.openConnection();
			
	        conn.setReadTimeout(10000 /* milliseconds */);
	        conn.setConnectTimeout(15000 /* milliseconds */);
	        conn.setRequestMethod("GET");
	        conn.setDoInput(true);
	        // Starts the query
	        conn.connect();
	        
	        is = conn.getInputStream();
	        res = readIt(is);
	        if (is != null) {
	            is.close();
	            conn.disconnect();
			}
	        
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
		}
		return res;
	}
	
	public String RestRequete(String method){
		return RestRequete(method,null);
		
	}
	
	public String readIt(InputStream is) throws IOException{
		StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = reader.readLine()) != null) {
          builder.append(line + "\n");
        }
    return builder.toString();
		
	}
}
